$(document).ready(function(){
	
	//根据屏幕大小设置字体大小
	var contentheight=document.getElementById("centreboxid").offsetHeight;
	
	if(contentheight>=500){
		$(".text1").css("font-size:25px");
		$(".text3").css("font-size:25px");
	}	
	
	$("#btn").on('click',function(){
		window.location.href="queryinventory.html";
	})
	
	$(document).on("click",".left-a",function(){
		var brandid=$(this).find(".B").find(".c").eq(1).text();
		var hjnum=$(this).find(".B").find(".c").eq(5).find(".gray").text();
		window.location.href="goodsshelves.html?brandid="+brandid+"&hjnum="+hjnum;
	})
	
    //返回上一页
    $('#returnlast').on("click",function(){
    	window.location.href="select.html";
    })
    //返回首页
    $("#returnpagehome").on("click",function(){
    	window.location.href="homepage.html";
    })	
    
    
	//验证传入的是否为null，undefined，NaN
	function checkWord(word) {
	    if (word === null || typeof (word) === "undefined") {
	        return "";
	    } else {
	        return word;
	    }
	};    
	
	
	function GetPercent(num, total) {
	    /// <summary>
	    /// 求百分比
	    /// </summary>
	    /// <param name="num">当前数</param>
	    /// <param name="total">总数</param>
	    num = parseFloat(num);
	    total = parseFloat(total);
	    if (isNaN(num) || isNaN(total)) {
	        return "-";
	    }
	    return total <= 0 ? "0%" : (Math.round(num / total * 10000) / 100.00)+"%";
	}	
	
	var zxkfkc=0;//中心库房品牌库存总金额
	var monthsellmoney=0;
	var kcmoney=0;
	var hjnum=0;//货架数
	var materialnum=0;//物料数
	
	
	//加载中心库房数据
	$.ajax({
        type: "post",
        url: "/mobile/gjapp/kufangdate/querykufangdate",
        dataType:"json",
        contentType:"application/json",
        success: function (data)
		{
			kfdata=data.data;
			
			if(data.state == 0)
			{
				//$("#sellmoney").text(returnFloat(kfdata[0].sellmoney));
				//$("#kcmoney").text(returnFloat(kfdata[0].kcmoney));
				//zxkfkc=kfdata[0].kcmoney;
				
				loadbranddata();

			}else{
				alert(data.msg)
			}
        }
	});		
	var a=['yucai.jpg','yutong.jpg','futian.jpg','jinlong.jpg','haige.jpg'];
	function loadbranddata(){
		//加载品牌区域数据
		$.ajax({
	        type: "post",
	        url: "/mobile/gjapp/brandarea/querybrandarea",
	        dataType:"json",
	        contentType:"application/json",
	        success: function (data)
			{
				//移除加载
				$(".text4").remove();				
				
				dataarr=data.data;

				if(data.state == 0)
				{
					for (var i=0;i<dataarr.length;i++) {
						zxkfkc=zxkfkc+dataarr[i].kcmoney;
					}	
					for (var i=0;i<dataarr.length;i++) {
						hjnum=hjnum+checkWord(dataarr[i].hjnum);
						monthsellmoney=monthsellmoney+dataarr[i].sellmoney;
						kcmoney=kcmoney+dataarr[i].kcmoney;
						materialnum=materialnum+checkWord(dataarr[i].typenum);
						$(".centrebox").append("<div class='left-a marginl'>"+
						"<div class='imgbox'> <img src='images/"+dataarr[i].brandid+".jpg'/></div>"+						
						"<div class='B'>"+
						"<div class='c'>"+
							"<div class='text3'>"+
								"<div class='aaa3'>销售：<span class='gray'>"+returnFloat(dataarr[i].sellmoney)+"</span></div>"+
							"</div>"+
						"</div>"+
						"<div class='c' style='display: none;'>"+
							"<div class='text3'>"+
								"<div class='aaa3'>"+dataarr[i].brandid+"</div>"+
							"</div>"+
						"</div>"+
						"<div class='c'>"+
							"<div class='text3'>"+
								"<div class='aaa3'>库存：<span class='gray'>"+returnFloat(dataarr[i].kcmoney)+"</span></div>"+
							"</div>"+
						"</div>"+
						"<div class='c'>"+
							"<div class='text3'>"+
								"<div class='aaa3'>库存占比：<span class='gray'>"+GetPercent(Math.abs(dataarr[i].kcmoney),Math.abs(zxkfkc))+"</span></div>"+
							"</div>"+
						"</div>"+
						"<div class='c'>"+
							"<div class='text3'>"+
								"<div class='aaa3'>品种数：<span class='gray'>"+dataarr[i].typenum+"</span></div>"+
							"</div>"+
						"</div>"+
						"<div class='c'>"+
							"<div class='text3'>"+
								"<div class='aaa3'>货架数：<span class='gray'>"+checkWord(dataarr[i].hjnum)+"</span></div>"+
							"</div>"+
						"</div>"+							
						"</div>"+
						"</div>");
						
						
					//不满5的倍数时，追加空元素占位
					if(dataarr.length<5){
						for (var i=0;i<5-dataarr.length;i++) {
							$(".centrebox").append("<div class='left-a marginl'>"+
							"<div class='imgbox'></div>"+						
							"<div class='B'>"+
							"<div class='c'>"+
								"<div class='text3'>"+
									"<div class='aaa3'><span class='gray'></span></div>"+
								"</div>"+
							"</div>"+
							"<div class='c' style='display: none;'>"+
								"<div class='text3'>"+
									"<div class='aaa3'></div>"+
								"</div>"+
							"</div>"+
							"<div class='c'>"+
								"<div class='text3'>"+
									"<div class='aaa3'><span class='gray'></span></div>"+
								"</div>"+
							"</div>"+
							"<div class='c'>"+
								"<div class='text3'>"+
									"<div class='aaa3'><span class='gray'></span></div>"+
								"</div>"+
							"</div>"+
							"<div class='c'>"+
								"<div class='text3'>"+
									"<div class='aaa3'><span class='gray'></span></div>"+
								"</div>"+
							"</div>"+
							"<div class='c'>"+
								"<div class='text3'>"+
									"<div class='aaa3'><span class='gray'></span></div>"+
								"</div>"+
							"</div>"+							
							"</div>"+
							"</div>");							
						}
					}
					
					
					
					//不满5的倍数时，追加空元素占位
					if(dataarr.length>5&&dataarr.length<10){
						for (var i=0;i<10-dataarr.length;i++) {
							$(".centrebox").append("<div class='left-a marginl'>"+
							"<div class='imgbox'></div>"+						
							"<div class='B'>"+
							"<div class='c'>"+
								"<div class='text3'>"+
									"<div class='aaa3'><span class='gray'></span></div>"+
								"</div>"+
							"</div>"+
							"<div class='c' style='display: none;'>"+
								"<div class='text3'>"+
									"<div class='aaa3'></div>"+
								"</div>"+
							"</div>"+
							"<div class='c'>"+
								"<div class='text3'>"+
									"<div class='aaa3'><span class='gray'></span></div>"+
								"</div>"+
							"</div>"+
							"<div class='c'>"+
								"<div class='text3'>"+
									"<div class='aaa3'><span class='gray'></span></div>"+
								"</div>"+
							"</div>"+
							"<div class='c'>"+
								"<div class='text3'>"+
									"<div class='aaa3'><span class='gray'></span></div>"+
								"</div>"+
							"</div>"+
							"<div class='c'>"+
								"<div class='text3'>"+
									"<div class='aaa3'><span class='gray'></span></div>"+
								"</div>"+
							"</div>"+							
							"</div>"+
							"</div>");							
						}
					}					
				    $("#sellmoney").text(returnFloat(monthsellmoney));
				    $("#kcmoney").text(returnFloat(kcmoney));	
					$("#hjnum").text(hjnum);
					$("#typenum").text(materialnum);
				}	
			}else{
				alert(data.msg);
			}
		}		
	});	
}
	
	
	//保留两位小数
	function returnFloat(value){
	    var value = Math.round(parseFloat(value) * 100) / 100;
	    var s = value.toString().split(".");
	if(s.length== 1){
	  value = value.toString() + ".00";
	  return value;
	}
	if (s.length > 1) {
	  if (s[1].length < 2) {
	   value = value.toString() + "0";
	    }
	    return value;
	   }
	}
	
})